package com.mfhcd.facepay.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.mfhcd.facepay.model.BaseResponseModel;
import com.mfhcd.facepay.model.Location;
import com.mfhcd.facepay.model.RequestModel;
import com.mfhcd.facepay.model.RequestModel.TrxDataReq;
import com.mfhcd.facepay.model.ResponseModel;
import com.mfhcd.facepay.network.api.NetworkRequest;
import com.mfhcd.facepay.network.callback.CallBack;
import com.mfhcd.facepay.service.LocationService;
import com.mfhcd.facepay.utils.DialogUtils;
import com.mfhcd.facepay.utils.ToastUtil;
import com.mfhcd.facepay.utils.Utils;

/**
 * Author：石强
 * E-Mail: shiqiang@mfhcd.com
 * Date：2019-09-09
 * 版权所有 © 现代金融控股（成都）有限公司
 */
public class TestViewModel extends BaseViewModel {
    private LocationService mLocationService;

    public TestViewModel(@NonNull Application application) {
        super(application);
    }

    /**
     * 获取banner数据
     *
     * @return
     */
    public MutableLiveData<ResponseModel.TestResp> test() {
        MutableLiveData mMutableLiveData = new MutableLiveData();
        RequestModel.TestReq requestModel = new RequestModel.TestReq();
        RequestModel.TestReq.Param requestParma = new RequestModel.TestReq.Param();
        requestParma.field1 = "1";
        requestParma.field2 = "2";
        requestParma.loginIP = Utils.getIpAddress(mContext);
        requestModel.setParam(requestParma);
        NetworkRequest.getInstance().builder(mContext).test(requestModel, new CallBack<BaseResponseModel<ResponseModel.TestResp>>() {
            @Override
            public void onSuccess(BaseResponseModel<ResponseModel.TestResp> adList) {
                mMutableLiveData.setValue(adList.data);
            }

            @Override
            public void onError(String code, String message) {

            }
        });
        return mMutableLiveData;
    }

    public MutableLiveData<Location> getLocationInfo() {
        MutableLiveData mMutableLiveData = new MutableLiveData();
        mLocationService = new LocationService(mActivity, new LocationService.LocationCallback() {
            @Override
            public void onLocationSuccess(Location location) {
                mMutableLiveData.setValue(location);
            }

            @Override
            public void onLocationFailure(String msg) {

            }
        });
        mLocationService.startLocation(LocationService.LOCATION_AMAP);
        return mMutableLiveData;
    }

    /**
     * 路由判断
     *
     * @param requestParas
     * @return
     */
    public MutableLiveData<ResponseModel.TrxDataResp> faceRouter(TrxDataReq requestParas) {
        MutableLiveData mMutableLiveData = new MutableLiveData();
        DialogUtils.showLoadingDialog(mContext);
        NetworkRequest.getInstance().builder(mContext).faceRouter(requestParas, new CallBack<ResponseModel.TrxDataResp>() {
            @Override
            public void onSuccess(ResponseModel.TrxDataResp responseModel) {
                DialogUtils.closeDialog();
                mMutableLiveData.setValue(responseModel);
            }

            @Override
            public void onError(String code, String message) {
                DialogUtils.closeDialog();
                ResponseModel.TrxDataResp dataResp = new ResponseModel.TrxDataResp();
                dataResp.setCode(code);
                dataResp.setMsg(message);
                mMutableLiveData.setValue(dataResp);
            }
        });
        return mMutableLiveData;
    }

    /**
     * 交易上送
     *
     * @param requestParas
     * @return
     */
    public MutableLiveData<ResponseModel.TransResp> doTransaction(RequestModel.TransReq requestParas) {
        MutableLiveData mMutableLiveData = new MutableLiveData();
        DialogUtils.showLoadingDialog(mContext);
        NetworkRequest.getInstance().builder(mContext).doTransaction(requestParas, new CallBack<ResponseModel.TransResp>() {
            @Override
            public void onSuccess(ResponseModel.TransResp responseModel) {
                DialogUtils.closeDialog();
                mMutableLiveData.setValue(responseModel);
            }

            @Override
            public void onError(String code, String message) {
                DialogUtils.closeDialog();
                ToastUtil.showToast("交易失败" + message);
                ResponseModel.TransResp resp = new ResponseModel.TransResp();
                resp.setCPSCOD(code);
                mMutableLiveData.setValue(resp);
            }
        });
        return mMutableLiveData;
    }

}
